import { useAccess } from '@vben/access'

export function usePermission() {
    const { hasAccessByCodes } = useAccess()
    function hasPermission(codes?: string | string[]) {
        if (!codes) return true

        if (Array.isArray(codes)) {
            return hasAccessByCodes(codes)
        }
        return hasAccessByCodes([codes])
    }

    return {
        hasPermission
    }
}

/**
 * 某个页面固定的权限前缀
 * @param pagePermissionKey
 */
export function usePagePermission(pagePermissionKey: string) {
    const { hasPermission } = usePermission()

    function getPagePermissionKey(key: string) {
        return `${pagePermissionKey}${key}`
    }

    function hasPagePermission(key: string) {
        return hasPermission(getPagePermissionKey(key))
    }

    return {
        hasPagePermission,
        hasPermission,
        getPagePermissionKey
    }
}
